package com.kuaimacode.kframework.admin.controller.score;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.score.AdminScoreManagentmentService;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.score.AdminScoreManagentment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Administrator
 * @date 2019/04/08
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/adminScoreManagentment"})
public class AdminScoreManagentmentController {
	private static String PAGE_FILE_NAME = "score/";

	@Autowired
	private AdminScoreManagentmentService adminScoreManagentmentServices;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<AdminScoreManagentment> pageInfo = adminScoreManagentmentServices.findAll(pageNum, pageSize);
		// pageInfo.list adminScoreManagentment
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		AdminScoreManagentment adminScoreManagentment = new AdminScoreManagentment();
		map.put("adminScoreManagentment", adminScoreManagentment);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit( ModelMap map) {
		AdminScoreManagentment adminScoreManagentment = adminScoreManagentmentServices.findOne();
		map.put("adminScoreManagentment", adminScoreManagentment);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(AdminScoreManagentment adminScoreManagentment, Boolean isAdd,ModelMap map) {
		try {
			Date now = new Date();
			if (adminScoreManagentment ==null||(adminScoreManagentment!=null&&adminScoreManagentment.getId()==null)){
				adminScoreManagentment.setStatus("1");
				adminScoreManagentment.setCreateDt(now);
				adminScoreManagentment.setUpdateDt(now);
				adminScoreManagentmentServices.save(adminScoreManagentment);
			}else{
				// 编辑保存
				adminScoreManagentment.setUpdateDt(now);
				adminScoreManagentmentServices.updateById(adminScoreManagentment);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		AdminScoreManagentment adminScoreManagentment = adminScoreManagentmentServices.getById(id);
		map.put("adminScoreManagentment", adminScoreManagentment);
		return PAGE_FILE_NAME+"detail";
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			adminScoreManagentmentServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
